﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LongSCADA_WPF.Helpers;
using LongSCADA_WPF.Models;
using ScottPlot;
using ScottPlot.WPF;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LongSCADA_WPF.ViewModels
{
    public partial class ChartViewModel : ObservableObject
    {
        private WpfPlot _wpfPlotShow;

        [ObservableProperty]
        DateTime _startime = DateTime.Now.AddDays(-30);

        [ObservableProperty]
        DateTime _endTime = DateTime.Now;

        /// <summary>
        /// 绘制图表
        /// </summary>
        /// <param name="wpfPlotShow"></param>
        public void Initplot(WpfPlot wpfPlotShow)
        {
            _wpfPlotShow = wpfPlotShow;
            ConfigurePlot();
        }
        /// <summary>
        /// 配置参考  https://scottplot.net/cookbook/5.0/
        /// </summary>
        private void ConfigurePlot()
        {
            if (_wpfPlotShow == null) return;
            // 配置图标样式
            _wpfPlotShow.Plot.Title("ScadaData Show");
            _wpfPlotShow.Plot.XLabel("Point");
            _wpfPlotShow.Plot.YLabel("Value");
            //显示图例 
            _wpfPlotShow.Plot.ShowLegend();

            Search();
        }

        [RelayCommand]
        void Search()
        {
            if (EndTime < Startime) return;
            try
            {
                //1.plot 清除所有数据
                _wpfPlotShow.Plot.Clear();

                // 2. 查找数据
                List<ScadaReadData> data = SqlSugarHelper.Db.Queryable<ScadaReadData>()
                    .Where(x => Startime <= x.CreateTime && x.CreateTime <= EndTime)
                    .OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
                    .ToList();
                //3.判断是否为空
                if (data.Count == 0) return;

                // 4.将数据添加到 plot 中
                float[] degreasingSprayPumpPressureY = data.Select(x => x.DegreasingSprayPumpPressure).ToArray();
                float[] fineWashSprayPumpPressureY = data.Select(x => x.FineWashSprayPumpPressure).ToArray();

                // 5.设置线条
                List<LinePattern> patterns = [];
                patterns.AddRange(LinePattern.GetAllPatterns());

                //6.添加数据
                var sig1 = _wpfPlotShow.Plot.Add.Signal(degreasingSprayPumpPressureY);
                sig1.LineWidth = 3;
                sig1.LegendText = "DegreasingSprayPumpPressure";
                sig1.LinePattern = patterns[1];

                var sig2 = _wpfPlotShow.Plot.Add.Signal(fineWashSprayPumpPressureY);
                sig2.LineWidth = 3;
                sig2.LegendText = "FineWashSprayPumpPressure";
                sig2.LinePattern = patterns[3];

                //7.缩放
                _wpfPlotShow.Plot.ScaleFactor = 1;

                //8.刷新显示
                _wpfPlotShow.Refresh();
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }
        }
    }
}
